import React, { useEffect, useState } from 'react'
import './index.scss'
import List from './list'
import Add from './add'
import Search from '../../components/search'
import axios from '../../utils/axios'
import { useHistory } from 'react-router-dom'
import qun from './images/qun.png'
import getTime from '../../utils/getTime'
import {loading} from '../../components/loading'
// import MediationRoom from './mediation-room'

export default function Home() {
  const [chatList, setChatList] = useState('')
  const history = useHistory()
 
  useEffect(() => {
    console.log(localStorage.getItem('userid'))
    getChatList()
  }, [])
  const getChatList = () => {
   let load =  loading()
    axios.get(`/chat/friendChat`).then((res) => {
      console.log(res.data.data,999)
      let arr = []
      for (let item of res.data.data) {
        console.log(item)
        let lastItem = item
        if (lastItem && lastItem.groupid) {
          lastItem.listType = 2
          // 群头像
          lastItem.avatarUrl = qun
          // 群名称
          lastItem.name = lastItem.groupName
          lastItem.listId = lastItem.groupid
          // lastItem.createTime = getTime(lastItem.createTime)
          arr.push(lastItem)
        } else {
          lastItem.listType = 1
          // lastItem.createTime = getTime(lastItem.createTime)
          if (lastItem.toid !== localStorage.getItem('userid')) {
            lastItem.avatarUrl = lastItem.toAvatar
            lastItem.name = lastItem.toName
            lastItem.friendid = lastItem.toid
          }
          if (lastItem.fromid !== localStorage.getItem('userid')) {
            lastItem.avatarUrl = lastItem.fromAvatar
            lastItem.name = lastItem.fromName
            lastItem.friendid = lastItem.fromid
          }
          lastItem.listId = lastItem.friendid
          arr.push(lastItem)
        }
      }
      // 排序
    
// 从大到小排序，越靠近现在的时间，时间戳越大
      arr.sort((a,b)=>{
        return new Date(b.createTime) - new Date(a.createTime)
      })
      for(let item of arr){
        item.createTime = getTime(item.createTime)
      }
      setChatList(arr)
      console.log(arr)
      load.close()
    }).catch(err=>{
      load.close()
    })
  }
  const toDetail = (id, name, type) => {
    // 1是朋友  2是群聊
    if (type === 1) {
      history.push(`/roomDetail?friendid=${id}&username=${name}`)
    }
    if (type === 2) {
      history.push(`/roomDetail?groupid=${id}&groupName=${name}`)
    }
  }
  return (
    <div className="home" id="home">
      <div className="home-header-text">全部聊天</div>
      <Add></Add>

      {/* <MediationRoom></MediationRoom> */}
      {/* <Search placeholderText="输入搜索聊天内容" toSubmit={submit}></Search> */}
      {chatList.length>0 ?
        chatList.map((item, index) => (
          <List
            toDetail={() => {
              toDetail(item.listId, item.name, item.listType)
            }}
            key={index.toString()}
            imgUrl={item.avatarUrl}
            name={item.name}
            text={item.content?.slice(0, 10)}
            time={item.createTime}
            unReadNumber={item.unReadNumber}
          ></List>
        )):<div className={'home-none-text'}>暂无消息</div>}
    </div>
  )
}
